use crate::arch::context::THREADS;

use super::{SysResult, Syscall};

impl Syscall<'_> {
    pub fn sys_yield(&mut self) -> SysResult {
        super::super::context::sched();
        Ok(0)
    }

    pub fn sys_exit(&mut self) -> SysResult {
        let mut threads = THREADS.lock();

        if let Some(pos) = threads
            .iter()
            .position(|thread| thread.read().get_id() == self.thread().get_id())
        {
            threads.remove(pos);
        }
        drop(threads);
        self.exit = true;
        Ok(0)
    }

    pub fn sys_getpid(&mut self) -> SysResult {
        Ok(self.thread().get_id())
    }
}
